<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { Component, inject, signal, OnInit<% if (microfrontend && enableTranslation && applicationTypeGateway) { %>, createNgModule, Injector<% } %> } from '@angular/core';
import { Router, RouterLink, RouterLinkActive } from '@angular/router';
<%_ if (enableTranslation) { _%>
import { TranslateService } from '@ngx-translate/core';
<%_ } _%>

import { StateStorageService } from 'app/core/auth/state-storage.service';
import SharedModule from 'app/shared/shared.module';
import HasAnyAuthorityDirective from 'app/shared/auth/has-any-authority.directive'
<%_ if (enableTranslation) { _%>
import { LANGUAGES } from 'app/config/language.constants';
import ActiveMenuDirective from './active-menu.directive';
<%_ } _%>
import { Account } from 'app/core/auth/account.model';
import { AccountService } from 'app/core/auth/account.service';
import { LoginService } from 'app/login/login.service';
import { ProfileService } from 'app/layouts/profiles/profile.service';
<% if (microfrontend && applicationTypeGateway) { %>
import { loadNavbarItems, loadTranslationModule } from 'app/core/microfrontend';
<%_ } _%>
import { environment } from 'environments/environment';
import NavbarItem from './navbar-item.model';

@Component({
  selector: '<%= jhiPrefixDashed %>-navbar',
  templateUrl: './navbar.html',
  styleUrl: './navbar.scss',
  imports: [
    RouterLink,
    RouterLinkActive,
    SharedModule,
    HasAnyAuthorityDirective,
<%_ if (enableTranslation) { _%>
    ActiveMenuDirective,
<%_ } _%>
  ],
})
export default class Navbar implements OnInit {
  inProduction = signal(true);
  isNavbarCollapsed = signal(true);
<%_ if (enableTranslation) { _%>
  readonly languages = LANGUAGES;
<%_ } _%>
  openAPIEnabled = signal(false);
  readonly version: string;
  account = inject(AccountService).trackCurrentAccount();
<%_ if (applicationTypeGateway && microfrontend) { _%>
  <%_ for (const remote of microfrontends) { _%>
  <%= remote.lowercaseBaseName %>EntityNavbarItems = signal<NavbarItem[]>([]);
  <%_ } _%>
<%_ } _%>

  private readonly loginService = inject(LoginService);
<%_ if (enableTranslation) { _%>
  private readonly translateService = inject(TranslateService);
  private readonly stateStorageService = inject(StateStorageService);
  <%_ if (applicationTypeGateway && microfrontend) { _%>
  private readonly injector = inject(Injector);
  <%_ } _%>
<%_ } _%>
<%_ if (applicationTypeGateway && microfrontend) { _%>
  private readonly accountService = inject(AccountService);
<%_ } _%>
  private readonly profileService = inject(ProfileService);
  private readonly router = inject(Router);

  constructor() {
    const { VERSION } = environment;
    if (VERSION) {
      this.version = VERSION.toLowerCase().startsWith('v') ? VERSION : `v${VERSION}`;
    } else {
      this.version = '';
    }
  }

  ngOnInit(): void {
    this.profileService.getProfileInfo().subscribe(profileInfo => {
      this.inProduction.set(profileInfo.inProduction ?? true);
      this.openAPIEnabled.set(profileInfo.openAPIEnabled ?? false);
    });

<%_ if (applicationTypeGateway && microfrontend) { _%>
    this.accountService.getAuthenticationState().subscribe(account => {
      this.loadMicrofrontendsEntities();
    });
<%_ } _%>
  }

<%_ if (enableTranslation) { _%>
  changeLanguage(languageKey: string): void {
    this.stateStorageService.storeLocale(languageKey);
    this.translateService.use(languageKey);
  }
<%_ } _%>

  collapseNavbar(): void {
    this.isNavbarCollapsed.set(true);
  }

  login(): void {
<%_ if (!authenticationTypeOauth2) { _%>
    this.router.navigate(['/login']);
<%_ } else { _%>
    this.loginService.login();
<%_ } _%>
  }

  logout(): void {
    this.collapseNavbar();
    this.loginService.logout();
    this.router.navigate(['']);
  }

  toggleNavbar(): void {
    this.isNavbarCollapsed.update(isNavbarCollapsed => !isNavbarCollapsed);
  }
<%_ if (applicationTypeGateway && microfrontend) { _%>

  loadMicrofrontendsEntities(): void {
    // Lazy load microfrontend entities.
  <%_ for (const remote of microfrontends) { _%>
    loadNavbarItems('<%= remote.lowercaseBaseName %>').then(
      async items => {
        this.<%= remote.lowercaseBaseName %>EntityNavbarItems.set(items);
    <%_ if (enableTranslation && applicationTypeGateway) { _%>
        try {
          const LazyTranslationModule = await loadTranslationModule('<%= remote.lowercaseBaseName %>');
          createNgModule(LazyTranslationModule, this.injector);
        } catch (error) {
          // eslint-disable-next-line no-console
          console.log('Error loading <%= remote.lowercaseBaseName %> translation module', error);
        }
    <%_ } _%>
      },
      (error: unknown) => {
        // eslint-disable-next-line no-console
        console.log('Error loading <%= remote.lowercaseBaseName %> entities', error);
      }
    );
  <%_ } _%>
  }
<%_ } _%>
}
